#define _CRT_SECURE_NO_WARNINGS  1


class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root)
    {
        vector<vector<int>> ret;
        if (nullptr == root)
            return ret;

        queue<TreeNode*> q;
        q.push(root);

        while (!q.empty())
        {

            vector<int> level;
            int levelSize = q.size();


            for (size_t i = 0; i < levelSize; ++i)
            {
                TreeNode* pCur = q.front();
                level.push_back(pCur->val);


                if (pCur->left)
                    q.push(pCur->left);

                if (pCur->right)
                    q.push(pCur->right);

                q.pop();
            }

            ret.push_back(level);
        }

        return ret;
    }
};